﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:apartmentBillsManager="clr-namespace:ApartmentBillsManager.Start"
                    xmlns:Converters="clr-namespace:ApartmentBillsManager.Start.Converters"
                    xmlns:manager="clr-namespace:ApartmentBillsManager.Start.Manager">

    <Converters:DateTimeNormalizer x:Key="DateTimeNormalizer"/>
    <Converters:RelevantRoommatesConverter x:Key="RelevantRoommatesConverter"/>
    <Converters:CategoryToHebrewConverter x:Key="CategoryToHebrewConverter"/>

    <DataTemplate DataType="{x:Type manager:ApartmentManager}">
        <Grid FlowDirection="RightToLeft">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10">
                <TextBlock Text="שם הדירה: " FontWeight="Bold"/>
                <TextBlock Text="{Binding LoadedApartment.AptName}"/>
            </StackPanel>

            <GroupBox Grid.Row="1" Padding="5" Margin="5">
                <GroupBox.HeaderTemplate>
                    <DataTemplate>
                        <Label Content="שותפים" FontWeight="Bold"></Label>
                    </DataTemplate>
                </GroupBox.HeaderTemplate>
                <ListView ItemsSource="{Binding LoadedApartment.Roommates}" Name="RoommatesList" Style="{DynamicResource ShortListViewStyle}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="שם" DisplayMemberBinding="{Binding Name}"/>
                            <GridViewColumn Header="תאריך כניסה" DisplayMemberBinding="{Binding DateOfEntrance, Converter={StaticResource DateTimeNormalizer}}"/>
                            <GridViewColumn Header="תאריך יציאה" DisplayMemberBinding="{Binding DateOfLeaving, Converter={StaticResource DateTimeNormalizer}}"/>
                        </GridView>
                    </ListView.View>
                </ListView>
            </GroupBox>

            <GroupBox Grid.Row="2" Padding="5" Margin="5">
                <GroupBox.HeaderTemplate>
                    <DataTemplate>
                        <Label Content="חשבונות" FontWeight="Bold"></Label>
                    </DataTemplate>
                </GroupBox.HeaderTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="6*"/>
                        <ColumnDefinition Width="5*"/>
                    </Grid.ColumnDefinitions>

                    <TabControl Grid.Column="0">

                        <TabItem Header="חשבונות פתוחים">
                            <StackPanel Orientation="Vertical">

                                <ListView ItemsSource="{Binding LoadedApartment.OpenBills}" SelectedItem="{Binding SelectedBill}" Name="OpenBillsList" Style="{DynamicResource LongListViewStyle}">
                                    <ListView.View>
                                        <GridView>
                                            <GridViewColumn Header="קטגוריה" DisplayMemberBinding="{Binding Category, Converter={StaticResource CategoryToHebrewConverter}}"/>
                                            <GridViewColumn Header="סכום" DisplayMemberBinding="{Binding Amount}"/>
                                            <GridViewColumn Header="תאריך התחלה" DisplayMemberBinding="{Binding StartDate, Converter={StaticResource DateTimeNormalizer}}"/>
                                            <GridViewColumn Header="תאריך סיום" DisplayMemberBinding="{Binding EndDate, Converter={StaticResource DateTimeNormalizer}}"/>
                                            <GridViewColumn Header="ימים" DisplayMemberBinding="{Binding TotalDays}"/>
                                            <GridViewColumn Header="המשלם" DisplayMemberBinding="{Binding Paid.Name}"/>
                                            <GridViewColumn Header="תאריך תשלום" DisplayMemberBinding="{Binding DatePaid, Converter={StaticResource DateTimeNormalizer}}"/>
                                            <GridViewColumn>
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <Button Content="עריכה" 
                                                                Command="{Binding DataContext.EditExistingBillCommand, ElementName=OpenBillsList}" 
                                                                CommandParameter="{Binding}"/>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>
                                        </GridView>
                                    </ListView.View>

                                    <ListView.ItemContainerStyle>
                                        <Style TargetType="ListViewItem">
                                            <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                        </Style>
                                    </ListView.ItemContainerStyle>
                                </ListView>

                                <StackPanel Orientation="Horizontal">
                                    <Button Content="הוסף חשבון חדש" Margin="0,5,0,0" Command="{Binding AddNewBillCommand}" IsEnabled="{Binding IsApartmentLoaded}"/>
                                </StackPanel>

                            </StackPanel>
                        </TabItem>

                        <TabItem Header="חשבונות סגורים">
                            <ListView ItemsSource="{Binding LoadedApartment.ClosedBills}" SelectedItem="{Binding SelectedBill}" Style="{DynamicResource LongListViewStyle}">
                                <ListView.View>
                                    <GridView>
                                        <GridViewColumn Header="קטגוריה" DisplayMemberBinding="{Binding Category, Converter={StaticResource CategoryToHebrewConverter}}"/>
                                        <GridViewColumn Header="סכום" DisplayMemberBinding="{Binding Amount}"/>
                                        <GridViewColumn Header="תאריך התחלה" DisplayMemberBinding="{Binding StartDate, Converter={StaticResource DateTimeNormalizer}}"/>
                                        <GridViewColumn Header="תאריך סיום" DisplayMemberBinding="{Binding EndDate, Converter={StaticResource DateTimeNormalizer}}"/>
                                        <GridViewColumn Header="ימים" DisplayMemberBinding="{Binding TotalDays}"/>
                                        <GridViewColumn Header="המשלם" DisplayMemberBinding="{Binding Paid.Name}"/>
                                        <GridViewColumn Header="תאריך תשלום" DisplayMemberBinding="{Binding DatePaid, Converter={StaticResource DateTimeNormalizer}}"/>
                                        <GridViewColumn Header="תאריך סגירה" DisplayMemberBinding="{Binding DateClosed, Converter={StaticResource DateTimeNormalizer}}"/>
                                    </GridView>
                                </ListView.View>
                            </ListView>
                        </TabItem>
                    </TabControl>

                    <StackPanel Grid.Column="1" Orientation="Vertical" Margin="5,0,0,0">

                        <TextBlock Text="תקופות בחשבון שנבחר" FontWeight="Bold" Margin="0,0,0,6"/>

                        <ListView ItemsSource="{Binding SelectedBill.Phases}" Style="{DynamicResource LongListViewStyle}">
                            <ListView.View>
                                <GridView>
                                    <GridViewColumn Header="תאריך התחלה" DisplayMemberBinding="{Binding StartDate, Converter={StaticResource DateTimeNormalizer}}"/>
                                    <GridViewColumn Header="תאריך סיום" DisplayMemberBinding="{Binding EndDate, Converter={StaticResource DateTimeNormalizer}}"/>
                                    <GridViewColumn Header="ימים" DisplayMemberBinding="{Binding TotalDays}"/>
                                    <GridViewColumn Header="השותפים המשתתפים" DisplayMemberBinding="{Binding RelevantRoommates, Converter={StaticResource RelevantRoommatesConverter}}"/>
                                    <GridViewColumn Header="סכום התקופה" DisplayMemberBinding="{Binding PartialAmount}"/>
                                </GridView>
                            </ListView.View>
                        </ListView>
                    </StackPanel>
                </Grid>
            </GroupBox>

            <GroupBox Grid.Row="3" Padding="5" Margin="5" Name="BalanceList">
                <GroupBox.HeaderTemplate>
                    <DataTemplate>
                        <Label Content="מאזן השותפים" FontWeight="Bold"></Label>
                    </DataTemplate>
                </GroupBox.HeaderTemplate>

                <ListView ItemsSource="{Binding Debts}" Style="{DynamicResource ShortListViewStyle}" >
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="חייב" DisplayMemberBinding="{Binding Path=From}"/>
                            <GridViewColumn Header="חייב ל-" DisplayMemberBinding="{Binding Path=To}"/>
                            <GridViewColumn Header="סכום" DisplayMemberBinding="{Binding Path=Amount}"/>
                            <GridViewColumn>
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Content="תשלום" 
                                                    Command="{Binding DataContext.PayDebtCommand, ElementName=BalanceList}" 
                                                    CommandParameter="{Binding}"/>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
            </GroupBox>
        </Grid>
    </DataTemplate>

</ResourceDictionary>